﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour,IDamageable
{
    EnemyBaseState currentState;//暂时没用

    BurningGhoul burning;

    [Header("Position Info")]
    public Transform playerPoint;
    public Transform leftPoint;
    public Transform rightPoint;
    public Transform targetPoint;
    public Transform attackTransform;

    protected Animator anim;
    protected Rigidbody2D rb;

    [Header("Enemy Setting")]
    public float speed;
    public float health;
    public float damage;

    [Header("Enemy State")]
    public bool isDead;
    public bool beAttack;
    
    protected virtual void Start()
    {
        burning = GetComponent<BurningGhoul>();
        anim = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
    }

    protected virtual void Update()
    {
        
    }

    private void FixedUpdate()
    {
        
    }

    public void GetHit(float damage)
    {
        health -= damage;
        if (health < 1)
        {
            health = 0;
            anim.Play("death");
            isDead = true;
        }
    }

    public void Death()
    {
        Destroy(gameObject);
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            //player进入检测范围时，添加为攻击目标
            if(attackTransform == null)
            {
                attackTransform = collision.transform;
            }
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            //Player离开检测范围时，清除攻击目标
            if(attackTransform != null)
            {
                attackTransform = null;
                burning.SwitchPoint();
            }
        }
    }
}
